$(function () {
    var obj = $('#seeNumber').val();
    var userId = $('#userId').val();
    var edit = $('#edit').val();
    var state = 0;
    if(obj){
        state = 1;
    }else{
        state = 0;
    }
    $('#myTable').dataTableExtend({
        "ajax": {
            "url": sysInfo.basePath + "/zs/com/com_list.do",
            "type": "post"
        }, "fnServerParams": function (aoData) {
            aoData = $.extend(aoData, $('.titleSearch').serialize2object());
            return aoData;
        },
        "pageLength": 10,//每页显示的条数默认是10条，有特殊业务需要显示不同条数的列表才应该加入该参数
        "scrollX": true,//是否开启横项滚动
        "fixedColumns": {//锁定列，只有开启scrollX时才生效
            "leftColumns": 3,//左侧锁定2列
            "rightColumns": 2//右侧锁定1列
        },
        "aoColumns": [
            {
                "sClass": "check-th",
                "title": "<input type=\"checkbox\" class=\"checkall\" value=\"undefined\">",
                "orderable": false,
                "data": "baseId",
                "render": function (data, type, full, meta) {
                    return '<input type="checkbox"  class="checkchild"  value="' + data + '" />';
                }
            },
            {
                "title": "序号",
                "orderable": false,
                "render": function (data, type, full, meta) {
                    return meta.row + 1;
                }
            },
            {
                "title": "公司名称",
                "data": "companyName",
                "orderable": false,
                "width":"120px",
                "render": function (data, type, full, meta) {
                    return data ?   '<div class="line2" style="min-width: 120px" title="'+data+'">'+data+'</div> ': ""
                }
            },
            {
                "title": "职位",
                "data": "position",
                "orderable": false,
                "width":"80px",
                "render": function (data, type, full, meta) {
                    return data ?  '<div class="line2" style="min-width: 80px" title="'+data+'">'+data+'</div> ': " ";
                }
            },{
                "title":"薪资",
                "data":"salaryMin",
                "orderable": false,
                "render":function(data, type, row, meta){
                    var str = "";
                    str +=  data ? data +"-" :"";
                    if(row.salaryMax){
                        str += row.salaryMax+"K";
                    }
                    return str;
                }
            }
            ,
            {
                "title": "人数",
                "data": "num",
                "orderable": false,
                "render": function (data, type, row, meta) {
                    return data;
                }
            },
            {
                "title": "行业",
                "data": "industry",
                "orderable": false,
                "width":"80px",
                "render": function (data, type, row, meta) {
                    return data ?  '<div class="line2" style="min-width: 80px" title="'+data+'">'+data+'</div> ': " ";
                }
            },
            {
                "title": "公司规模",
                "data": "scale",
                "orderable": false,
                "render": function (data, type, row, meta) {
                    return data;
                }
            },
            {
                "title": "合同号",
                "data": "contractNo",
                "orderable": false,
                "width":"80px",
                "render": function (data, type, row, meta) {
                    return data ?  '<div class="line2" style="min-width: 80px" title="'+data+'">'+data+'</div> ': " ";
                }

            },
            {
                "title": "合同周期",
                "data": "contractStartDate",
                "orderable": false,
                "render":function(data,type,row,meta){
                    if(null == data){
                        return '-';
                    }else{
                        data = new Date(data).getTime();
                        endDate = new Date(row.contractEndDate).getTime()
                        return   new Date(data).format("yyyyMMdd")+"—"+ new Date(endDate).format("yyyyMMdd");
                    }
                }
            },
            {
                "title": "联系人",
                "data": "contactName",
                "orderable": false,
                "width":"70px",
                "render":function(data,type,row,meta){
                    var html = '<p><span class="line2" style="min-width: 40px" title="'+data+'">'+data+'</span></p> ' + '<p>' + emptyStringConvert(row.contactPhone) + '</p>'
                    if(state){
                        html+= '<a href="javascript:void(0)" class="opt-icon look"onclick="seeNumber(this,\''+row.id+'\')"></a>';
                    }
                    return html;
                }
            },
            {
                "title": "跟踪人",
                "data": "trackerUserName",
                "orderable": false
            },
            {
                "title": "合同状态",
                "data": "cooperateStatus",
                "orderable": false
            },
            {
                "title": "匹配人才",
                "data": "entryTalent",
                "width":"80px",
                "orderable": false,
                "render": function (data, type, row, meta) {
                   return data ?  '<div class="line2" style="min-width: 80px" title="'+data+'">'+data+'</div> ' : "";
                }
            }
            ,
            {
                "title": "岗位职责",
                "data": "jobDuty",
                "orderable": false,
                "width":"80px",
                "render": function (data, type, row, meta) {
                    return  '<a href="javascript:void(0)" ti  onclick="viewJobDuty(\'' + row.id + '\')" >'+'<div class="line2 layui-table-link" style="min-width: 80px" title="'+data+'">'+data+'</div> '+'</a>';
                }
            },
            {
                "title": "简历附件",
                "data": "files",
                "orderable": false,
                "width":"120px",
                "render": function (data, type, row, meta) {
                   return data ?   '<div class="line2" style="min-width: 120px" title="'+data+'">'+data+'</div> ': ""
                }
            },
            {
                "title": "操作",
                "orderable": false,
                "data": "id",
                "width": "190",
                "render": function (d, type, r, meta) {
                    var result = "";
                    //打电话
                    result += '<a href=\'javascript:void(0)\' ti  onclick="editCom(\'' + r.id + '\')" /><span class="layui-table-link">编辑</a></span>&nbsp;';
                    result += '<a href=\'javascript:void(0)\' ti  onclick="updateState(\'' + r.id + '\')" /><span class="layui-table-link">状态更新</a></span>';//备注
                    return result;
                }
            }
        ],
    });
});

/**
 *调新建页面
 */
function create(){
    DGG.loading();
    $.post(sysInfo.basePath + "/zs/com/creatCom.html", function (data) {
        dgg.Open({
            title: '新增公司',
            content: data,
            area: '700px',
            btn: ["确定", "取消"],
            yes: function () {
                talentCreate();
            }, btn2: function () {
                DGG.loading("close");
            }
        });
    }, 'html');
}
function editCom(id){
    DGG.loading();
    $.post(sysInfo.basePath + "/zs/com/editCom.html",{id:id}, function (data) {
        dgg.Open({
            title: '编辑公司',
            content: data,
            area: '700px',
            btn: ["确定", "取消"],
            yes: function () {
                saveTalent();
            }, btn2: function () {
                DGG.loading('close');
            }
        });
    }, 'html');

}
function saveTalent(){
    var companyName = $('[name="companyName"]').val();
    var industry = $('[name="industry"]').val();
    var contactName = $('[name="contactName"]').val();
    var contactPhone = $('[name="contactPhone"]').val();
    var salaryMin  = $('[name="salaryMin"]').val();
    var salaryMax = $('[name="salaryMax"]').val();
    var oldPhone = $('[name="oldPhone"]').val();
    var num = $('[name="num"]').val();
    if(isEmpty(companyName)){
        DGG.Warning('公司名称不能为空！');
        return;
    }
    if(isEmpty(industry)){
        DGG.Warning('行业不能为空！');
        return;
    }
    if(isEmpty(contactName)){
        DGG.Warning('联系人不能为空！');
        return;
    }
    if(isEmpty(contactPhone)){
        DGG.Warning('联系人电话不能为空！');
        return;
    }
   if(oldPhone != contactPhone){
       if(!isTel($("#contactPhone"),true)){
           return;
       }
   }
    if(!isEmpty(num)){
        if(!isInterval($("#num"),100,1,true)){
            return;
        };
    }
    if(!isEmpty(salaryMin)){
        if(!isInterval($("#salaryMin"),5000,1,true)){
            return;
        };
    }
    if(!isEmpty(salaryMax)){
        if(!isInterval($("#salaryMax"),5000,1,true)){
            return;
        };
    }
    if(!isEmpty(salaryMin) && !isEmpty(salaryMax)){
        if(salaryMin*1>salaryMax*1){
            DGG.Warning('薪资后面的值大于前面的值！');
            return;
        }
    }

    var params=$('#flowCreate').serializeArray();
    DGG.Confire("确定编辑企业库？", '编辑企业库', function () {
        DGG.loading();
        $.ajax({
            type: 'POST',
            async: true,
            data: params,
            url: sysInfo.basePath + '/zs/com/saveCom.do',
            dataType: 'JSON',
            success: function (data) {
                DGG.loading('close');
                if (data.code == 0) {
                    DGG.Succeed('操作成功！')
                    setTimeout(function () {
                        layer.closeAll();
                    }, 1000);
                    reloadTable1();
                } else {
                    DGG.Errors(data.msg);
                }
            },
            error: function () {
                DGG.Errors('保存失败，请联系管理员！！');
            }
        })
    }, function () {
    })
}
function talentCreate(){
    var companyName = $('[name="companyName"]').val();
    var industry = $('[name="industry"]').val();
    var contactName = $('[name="contactName"]').val();
    var contactPhone = $('[name="contactPhone"]').val();
    var salaryMin  = $('[name="salaryMin"]').val();
    var salaryMax = $('[name="salaryMax"]').val();
    var num = $('[name="num"]').val();
    if(isEmpty(companyName)){
       DGG.Warning('公司名称不能为空！');
        return;
    }
    if(isEmpty(industry)){
        DGG.Warning('行业不能为空！');
        return;
    }
    if(isEmpty(contactName)){
        DGG.Warning('联系人不能为空！');
        return;
    }
    if(isEmpty(contactPhone)){
        DGG.Warning('联系人电话不能为空！');
        return;
    }
    if(!isTel($("#contactPhone"),true)){
        return;
    }
    if(!isEmpty(num)){
        if(!isInterval($("#num"),100,1,true)){
            return;
        };
    }
    if(!isEmpty(salaryMin)){
        if(!isInterval($("#salaryMin"),5000,1,true)){
            return;
        };
    }
    if(!isEmpty(salaryMax)){
        if(!isInterval($("#salaryMax"),5000,1,true)){
            return;
        };
    }
    if(!isEmpty(salaryMin) && !isEmpty(salaryMax)){
        if(salaryMin*1>salaryMax*1){
            DGG.Warning('薪资后面的值大于前面的值！');
            return;
        }
    }
    var params=$('#flowCreate').serializeArray();
    DGG.Confire("确定新增公司库？", '新增公司库', function () {
        DGG.loading();
        $.ajax({
            type: 'POST',
            async: true,
            data: params,
            url: sysInfo.basePath + '/zs/com/addCom.do',
            dataType: 'JSON',
            success: function (data) {
                DGG.loading('close');
                if (data.code == 0) {
                    DGG.Succeed('操作成功！')
                    setTimeout(function () {
                        layer.closeAll();
                    }, 1000);
                    reloadTable1();
                } else {
                    DGG.Errors(data.msg);
                }
            },
            error: function () {
                DGG.Errors('保存失败，请联系管理员！！');
            }
        })
    }, function () {
    })
}

function reloadTable1() {
    $('#myTable').dataTableExtend("reload");
}
function emptyStringConvert(str) {
    if (null == str || '' == str) {
        return '';
    } else {
        return str;
    }
}

/**
 * c
 * @param obj
 * @param number
 * @param type
 * @param getType
 */
function seeNumber(obj,id) {
    $.ajax({
        url: sysInfo.basePath + "/zs/com/see_number.do",
        data: {id:id},
        type: "POST",
        dataType: "json",
        success: function (result) {
            if (result.code == 0) {
                console.log($(obj));
                var $obj = $(obj).prev();

                var beforeNumber = $obj.html();
                var number = result.data;
                $obj.html(number);
                $(obj).hide();
                DGG.Layer.message("5秒后自动隐藏", {icon: '2'});
                window.setTimeout(function () {
                    $obj.html(beforeNumber);
                    $(obj).show();
                }, 5000);
                DGG.Layer.message("5秒后自动隐藏", {icon: '2'});
            } else {
                DGG.Layer.message(result.msg, {icon: '2'});
            }
        }
    });
}
// 打电话
function call() {
    var dataArr = $('#myTable').dataTableExtend('getCheck');
    if (dataArr.length == 0) {
        DGG.Warning('请选择一条订单')
        return false;
    }
    if (dataArr.length > 1) {
        DGG.Warning('只能选择一条订单');
        return false;
    }
    callPhoneByTalent(dataArr[0].id);
}
function callPhoneByTalent(id) {
    if (!id) {
        DGG.Warning('ID为空！');
        return;
    }
    DGG.loading();
    $.ajax({
        url: sysInfo.basePath + "/zs/com/find_com.do",
        data: {id: id},
        type: "POST",
        dataType: "json",
        success: function (result) {
            DGG.loading('close');
            if (result.code == 0) {
                var talentInfo = result.data;
                if (talentInfo) {
                    callPhone(talentInfo.id,talentInfo.name,talentInfo.phone,talentInfo.phoneStr,'');
                } else {
                    DGG.Errors("号码不可拨打!");
                }
            } else {
                DGG.Errors(result.msg);
            }
        }
    });
}
function downFile(){
    var dataArr = $('#myTable').dataTableExtend('getCheck');
    if (dataArr.length == 0) {
        DGG.Warning('请选择一条订单')
        return false;
    }
    if (dataArr.length > 1) {
        DGG.Warning('只能选择一条订单');
        return false;
    }
    if(dataArr[0].files) {
        downLoadPage(dataArr[0].id);
    }else{
        DGG.Warning('没有合同附件！');
        return false;
    }
}
function downLoadPage(id){
    DGG.loading();
    $.post(sysInfo.basePath + "/zs/com/downLoadFile.html",{id:id}, function (data) {
        dgg.Open({
            title: '合同下载',
            content: data,
            area: ['800px','800px'],
            btn: ["取消"],
            btn2: function () {

            }
        });
    }, 'html');
    DGG.loading("close");
}
function isEmpty(obj) {
    if (typeof obj == "undefined" || obj == null || obj == "") {
        return true;
    } else {
        return false;
    }
}
function allotTracker(){
    var dataArr = $('#myTable').dataTableExtend('getCheck');
    if (dataArr.length == 0) {
        DGG.Warning('请选择一条订单')
        return false;
    }
    if (dataArr.length > 1) {
        DGG.Warning('只能选择一条订单');
        return false;
    }
    var id = dataArr[0].id;
    DGG.loading();
    $.post(sysInfo.basePath + "/zs/com/trackerPage.html",{id:id}, function (data) {
        dgg.Open({
            title: '分配跟踪人',
            content: data,
            area: '500px',
            btn: ["确定", "取消"],
            yes: function () {
                updateTracker();
            }, btn2: function () {
                DGG.loading('close');
            }
        });
    }, 'html');
}
function updateTracker(){
    var trackerId = $("#trackerId").val();
    if(isEmpty(trackerId)){
        DGG.Warning('请选择人员！');
        return;
    }
    var params=$('#flowCreate').serializeArray();
    DGG.Confire("确定分配跟踪人？", '分配跟踪人', function () {
        DGG.loading();
        $.ajax({
            type: 'POST',
            async: true,
            data: params,
            url: sysInfo.basePath + '/zs/com/update.do',
            dataType: 'JSON',
            success: function (data) {
                DGG.loading('close');
                if (data.code == 0) {
                    DGG.Succeed('操作成功！')
                    setTimeout(function () {
                        layer.closeAll();
                    }, 1000);
                    reloadTable1();
                } else {
                    DGG.Errors(data.msg);
                }
            },
            error: function () {
                DGG.Errors('保存失败，请联系管理员！！');
            }
        })
    }, function () {
    })
}
function updateState(id){
    DGG.loading();
    $.post(sysInfo.basePath + "/zs/com/statePage.html",{id:id}, function (data) {
        dgg.Open({
            title: '更新合同状态',
            content: data,
            area: '500px',
            btn: ["确定", "取消"],
            yes: function () {
                doUpdateState();
            }, btn2: function () {
                DGG.loading('close');
            }
        });
    }, 'html');
}
function doUpdateState(){
    var params=$('#flowCreate').serializeArray();
    DGG.Confire("确定修改合同状态？", '修改合同状态', function () {
        DGG.loading();
        $.ajax({
            type: 'POST',
            async: true,
            data: params,
            url: sysInfo.basePath + '/zs/com/update.do',
            dataType: 'JSON',
            success: function (data) {
                DGG.loading('close');
                if (data.code == 0) {
                    DGG.Succeed('操作成功！')
                    setTimeout(function () {
                        layer.closeAll();
                    }, 1000);
                    reloadTable1();
                } else {
                    DGG.Errors(data.msg);
                }
            },
            error: function () {
                DGG.Errors('保存失败，请联系管理员！！');
            }
        })
    }, function () {
    })
}
function viewJobDuty(id){
    DGG.loading();
    $.post(sysInfo.basePath + "/zs/com/viewJobDuty.html",{id:id}, function (data) {
        dgg.Open({
            title: '查看岗位职责',
            content: data,
            area: '800px',
            btn: ["取消"],
            btn2: function () {
                DGG.loading('close');
            }
        });
    }, 'html');
    DGG.loading('close');
}
//导出办理中数据
function exportIprocessData() {
    var paras = $('.titleSearch').serialize2object();
    if (Object.keys(paras).length == 0) {
        DGG.Warning('请加入搜索条件后导出！');
        return;
    }
    $.extend(paras, {type: $(".title-tab").find("a.active").attr("data-status")});
    exportDataAbandoned('/zs/com/export_company.do', paras);
}

